//
//  ZLSessionListCell.m
//  ZLYoYoIM_Example
//
//  Created by 赵磊 on 2021/1/15.
//  Copyright © 2021 itzhaolei. All rights reserved.
//

#import "ZLSessionListCell.h"

@implementation ZLSessionListCell

#pragma mark - Lazy
- (NIMAvatarImageView *)iconImageView {
    if (!_iconImageView) {
        NIMAvatarImageView *imageView = [[NIMAvatarImageView alloc] initWithFrame:CGRectMake(12.0, 13.0, 44.0, 44.0)];
        imageView.layer.cornerRadius = 10.0;
        imageView.layer.masksToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        imageView.backgroundColor = [UIColor colorWithRed:(245 + (arc4random()%10)) / 255.0 green:(245 + (arc4random()%10)) / 255.0 blue:(245 + (arc4random()%10)) / 255.0 alpha:1.0];
        imageView.userInteractionEnabled = YES;
        [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickIconAction)]];
        [self.contentView addSubview:imageView];
        _iconImageView = imageView;
    }
    return _iconImageView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.iconImageView.frame) + 12.0, CGRectGetMinY(self.iconImageView.frame) + 2.0, UIScreen.mainScreen.bounds.size.width - CGRectGetMaxX(self.iconImageView.frame) - 12.0 - 90.0 - 12.0, 17.0)];
        label.font = [UIFont boldSystemFontOfSize:16.0];
        [self.contentView addSubview:label];
        _titleLabel = label;
    }
    return _titleLabel;
}
- (UILabel *)subTitleLabel {
    if (!_subTitleLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.iconImageView.frame) + 12.0, CGRectGetMaxY(self.iconImageView.frame) - 15.0, UIScreen.mainScreen.bounds.size.width - CGRectGetMaxX(self.iconImageView.frame) - 24.0, 15.0)];
        label.font = [UIFont systemFontOfSize:14.0];
        label.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0];
        [self.contentView addSubview:label];
        _subTitleLabel = label;
    }
    return _subTitleLabel;
}
- (UILabel *)timeLabel {
    if (!_timeLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.titleLabel.frame), CGRectGetMinY(self.titleLabel.frame), 90.0, 17.0)];
        label.font = [UIFont systemFontOfSize:12.0];
        label.textColor = [UIColor colorWithRed:102/255.0 green:102/255.0 blue:102/255.0 alpha:1.0];
        label.textAlignment = NSTextAlignmentRight;
        [self.contentView addSubview:label];
        _timeLabel = label;
    }
    return _timeLabel;
}
- (UILabel *)badgeLabel {
    if (!_badgeLabel) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.iconImageView.frame) - 10.0, CGRectGetMinY(self.iconImageView.frame) - 10.0, 20.0, 20.0)];
        label.font = [UIFont boldSystemFontOfSize:10.0];
        label.textColor = UIColor.whiteColor;
        label.backgroundColor = UIColor.redColor;
        label.layer.cornerRadius = CGRectGetHeight(label.frame) / 2.0;
        label.layer.masksToBounds = YES;
        label.layer.borderColor = UIColor.whiteColor.CGColor;
        label.layer.borderWidth = 2.0;
        label.textAlignment = NSTextAlignmentCenter;
        label.hidden = YES;
        [self.contentView addSubview:label];
        _badgeLabel = label;
    }
    return _badgeLabel;
}
- (CALayer *)bottomLine {
    if (!_bottomLine) {
        CALayer *layer = [CALayer layer];
        layer.backgroundColor = [UIColor colorWithRed:238/255.0 green:238/255.0 blue:238/255.0 alpha:1.0].CGColor;
        layer.frame = CGRectMake(12.0, 69.5, UIScreen.mainScreen.bounds.size.width - 24.0, 0.5);
        [self.contentView.layer addSublayer:layer];
        _bottomLine = layer;
    }
    return _bottomLine;
}

#pragma mark - Action
- (void)clickIconAction {
    if (self.clickIcon) {
        self.clickIcon();
    }
}

#pragma mark - ReuseAndCreate
+ (instancetype)reuseCell:(UITableView *)tableView {
    ZLSessionListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZLSessionListCell class])];
    if (!cell) {
        cell = [[self alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:NSStringFromClass([ZLSessionListCell class])];
        cell.selectionStyle = NO;
        [cell bottomLine];
    }
    return cell;
}

@end
